<template>
<!-- 报修与维修 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师" prop="teacherId">
        <!-- <el-input v-model="queryParams.teacherId" placeholder="请输入教师id" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.teacherId" placeholder="请选择教师">
            <el-option :label="item.userName" :value="item.userId" v-for="(item,index) in userList" :key="index" v-if="item.roleId!=2" />
        </el-select>
      </el-form-item>
      <el-form-item label="实验室" prop="labId">
        <!-- <el-input v-model="queryParams.labId" placeholder="请输入实验室id" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.labId" placeholder="请选择实验室名称">
            <el-option :label="item.labName" :value="item.labId" v-for="(item,index) in laboratoryList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="报修日期" prop="reportDate">
        <el-date-picker clearable v-model="queryParams.reportDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择报修日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['lab:repair:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['lab:repair:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['lab:repair:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['lab:repair:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维修表唯一id
" align="center" prop="repairId" />
      <el-table-column label="教师名" align="center" prop="teacherName" />
      <el-table-column label="实验室名" align="center" prop="labName" />
      <el-table-column label="故障描述" align="center" prop="description" />
      <el-table-column label="报修日期" align="center" prop="reportDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报修状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['lab:repair:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['lab:repair:remove']">删除</el-button>


            <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['lab:repair:changeStatus']">
                <el-button size="mini" type="text" icon="el-icon-d-arrow-right">修改状态</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="handleUpdateStatus" icon="el-icon-circle-check"
                    v-hasPermi="['lab:repair:changeStatus']">维修状态修改</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>

        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 添加或修改实验室维修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="教师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师id" />
        </el-form-item> -->
        <el-form-item label="实验室" prop="labId">
          <!-- <el-input v-model="form.labId" placeholder="请输入实验室id" /> -->
          <el-select v-model="form.labId" placeholder="请选择实验室名称">
            <el-option :label="item.labName" :value="item.labId" v-for="(item,index) in laboratoryList" :key="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报修日期" prop="reportDate">
          <el-date-picker clearable v-model="form.reportDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择报修日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 下面是状态修改的对话框对话框 -->
    <el-dialog :title="title" :visible.sync="statusOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择维修状态">
            <el-option label="已维修" value="0" />
            <el-option label="未维修" value="1" />
            <el-option label="维修中" value="2" />
          </el-select>
        </el-form-item> 

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepair, getRepair, delRepair, addRepair, updateRepair } from "@/api/lab/repair";
import {listLaboratory} from "@/api/lab/laboratory";
import {listUser} from "@/api/system/user";

export default {
  name: "Repair",
  data() {
    return {
      userList:[],
      laboratoryList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验室维修表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否弹出状态修改对话框
      statusOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        labId: null,
        description: null,
        reportDate: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAllList();
    this.getUserList();
  },
  methods: {
    /** 查询用户列表 */
    async getUserList() {
        await listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          console.log(this.userList);
        }
      );
    },
    /** 查询实验室存储管理列表 */
    async getAllList() {
      // this.loading = true;
      await listLaboratory(this.queryParams).then(response => {
        this.laboratoryList = response.rows;
        console.log(this.laboratoryList);
        // this.total = response.total;
        // this.loading = false;
      });
    },
    /** 查询实验室维修列表 */
    getList() {
      this.loading = true;
      listRepair(this.queryParams).then(response => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.statusOpen= false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        repairId: null,
        teacherId: null,
        labId: null,
        description: null,
        reportDate: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.repairId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验室维修";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const repairId = row.repairId || this.ids
      getRepair(repairId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验室维修";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.repairId != null) {
            updateRepair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.statusOpen=false;
              this.getList();
            });
          } else {
            addRepair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.statusOpen=false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const repairIds = row.repairId || this.ids;
      this.$modal.confirm('是否确认删除实验室维修编号为"' + repairIds + '"的数据项？').then(function () {
        return delRepair(repairIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lab/repair/export', {
        ...this.queryParams
      }, `repair_${new Date().getTime()}.xlsx`)
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdateStatus":
          this.handleUpdateStatus(row);
          break;
        default:
          break;
      }
    },
     /** 更改维修状态 */
     handleUpdateStatus: function(row) {
      this.reset();
      const repairId = row.repairId || this.ids
      getRepair(repairId).then(response => {
        this.form = response.data;
        this.statusOpen = true;
        this.title = "修改实验室维修";
      });
    }
  }
};
</script>
